﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ABTool : AssetPostprocessor {
	////模型导入之前调用  
	public void OnPreprocessModel()  
	{  
//		Debug.Log ("OnPreprocessModel="+this.assetPath);  
	}  
	////模型导入之后调用  
	void OnPostprocessModel( GameObject input ){
        // Debug.Log ("OnPostprocessModel="+go.name);  
	}

    //纹理导入之前调用，针对纹理进行设置  
    public void OnPreprocessTexture()  
    {
        //Debug.Log("OnPreProcessTexture=" + this.assetPath);  
        //TextureImporter importer = this.assetImporter as TextureImporter;
        ////只自动设置图集里的图片类型，其他图如法线纹理不自动设置
        //if (this.assetPath.Contains("atlas/")) {
        //    importer.textureType = TextureImporterType.Sprite;
        //}

//        impor.textureFormat = TextureImporterFormat.ARGB32;  
//        impor.maxTextureSize = 512;  
//        impor.textureType = TextureImporterType.Advanced;  
//        impor.mipmapEnabled = false;  
			
  
    }  
    //纹理导入之后调用
    public void OnPostprocessTexture(Texture2D tex)  
    {  
//         Debug.Log ("OnPostProcessTexture="+this.assetPath);  
		string str=this.assetPath;
		TextureImporter importer = this.assetImporter as TextureImporter;  
		
        
        string dirName = System.IO.Path.GetDirectoryName(assetPath);  
        string folderStr = System.IO.Path.GetFileName(dirName);  
        // Debug.Log("Import ---  "+dirName);  
        // Debug.Log("Import ---  "+folderStr);  
        
		// int startIndex = str.IndexOf ("atlas") + 5;
		// string fileTypeName = "";
		// char[] chars = str.ToCharArray ();
		// for (int i = 0; i < chars.Length; i++) {
		// 	if (chars [i] == '/'&&i>startIndex) {
		// 		fileTypeName = str.Substring (startIndex + 1, i - startIndex - 1);
		// 		break;
		// 	}
		// }


        //只有UI图会根据其所在目录自动设置ab和图集标签
        //if (this.assetPath.Contains("atlas/"))
        //{
        //    importer.textureType = TextureImporterType.Sprite;
        //    //importer.spriteImportMode = SpriteImportMode.Single;

        //    importer.assetBundleName = "atlas/" + folderStr;
        //    importer.spritePackingTag = folderStr;
        //}
        //else {
        //    importer.assetBundleName = "";
        //    importer.spritePackingTag = "";
        //    importer.maxTextureSize = 1024;
        //    importer.textureCompression = TextureImporterCompression.Compressed;
        //}
    }  

    //音频导入前
    public void OnPreprocessAudio()  
    {  
        // AudioImporter audio = this.assetImporter as AudioImporter;  
        // audio.format = AudioImporterFormat.Compressed;  
    } 

    //音频导入后
    public void OnPostprocessAudio(AudioClip clip)  
    {  
      
    }   

	//所有的资源的导入，删除，移动，都会调用此方法 
	public static void OnPostprocessAllAssets(string[]importedAsset,string[] deletedAssets,string[] movedAssets,string[]movedFromAssetPaths) {
        foreach (string str in importedAsset) {  
            Debug.Log("importedAsset = "+str);  
//			if (str.Contains ("GameResources/UI") && str.EndsWith (".prefab")) {
//				AssetImporter assetImporter = AssetImporter.GetAtPath (str);
////				Debug.Log(str.Substring(str.LastIndexOf('/'),str.LastIndexOf('.')-str.LastIndexOf('/')));
//				assetImporter.assetBundleName = "ui/" +GetEndName(str) ;
//			}else if(str.ToLower().EndsWith(".unity")){
//				AssetImporter assetImporter = AssetImporter.GetAtPath (str);
//				assetImporter.assetBundleName = "scene/"+GetEndName(str);
//			}
//			else if (str.Contains ("GameResources/atlas") && (str.EndsWith (".jpg") || str.EndsWith (".png"))) {
//				AssetImporter assetImporter = AssetImporter.GetAtPath (str);
//				int startIndex = str.IndexOf ("atlas") + 5;
//				string fileTypeName = "";
//				char[] chars = str.ToCharArray ();
//				for (int i = 0; i < chars.Length; i++) {
//					if (chars [i] == '/'&&i>startIndex) {
//						fileTypeName = str.Substring (startIndex + 1, i - startIndex - 1);
//						break;
//					}
//				}
//				assetImporter.assetBundleName = "atlas/" +fileTypeName;
//			}
        }  
        foreach (string str in deletedAssets) {  
            Debug.Log("deletedAssets = "+str);  
        }  
        foreach (string str in movedAssets) {  
            Debug.Log("movedAssets = "+str);  
        }  
        foreach (string str in movedFromAssetPaths) {  
            Debug.Log("movedFromAssetPaths = "+str);  
        }  
	}

	static string GetEndName(string str){
		return  str.Substring (str.LastIndexOf ('/') + 1, str.LastIndexOf ('.') - str.LastIndexOf ('/') - 1);
	}
}
